<template>
	<view>
		<u-swiper :list="slides" name="img_url" height="320"></u-swiper>
		<view class="u-text-center u-m-t-30">
			<u-tabs 
			:list="sortList"   
			:current="currentSort"
			:bar-width="80"
			:item-width="160"
			@change="changeSort"></u-tabs>
		</view>
		
		<view class="wrap u-m-t-20 u-skeleton">
			<u-row gutter="20">
				<u-col span="6" v-for="(item,index) in goods.length ? goods:4" :key="index">
					<goods-card :item="item"></goods-card>
				</u-col>
			</u-row>
			<u-skeleton :loading="loading" :animation="true" bgColor="#FFF"></u-skeleton>	 
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				slides:[],
				goods:[],
				sortList:[
					{name:"默认"},
					{name:"销量"},
					{name:"推荐"},
					{name:"最新"}
				],
				currentSort:0,
				page:1,
				loading:false
			}
		},
		async mounted() {
			
		},
		async onLoad(options) {
		  
		  this.getData()
		},
		onReachBottom() {
		    this.page++
			this.getData()
		},
		methods: {
			changeSort(index){
				this.currentSort = index;
				this.page = 1;
				this.goods = [];
				this.getData();
			},
			async getData(){
				if(this.page == 1) this.loading = true; //显示骨架屏
				const params = {
					page:this.page
				}
				if(this.currentSort == 1) params.sales = 1
				if(this.currentSort == 2) params.recommend = 1
				if(this.currentSort == 3) params.new = 1
				const result = await this.$u.api.index(params);
				if(this.page == 1) this.loading = false;//隐藏骨架屏
				this.slides = result.slides
				this.goods = [...this.goods,...result.goods.data]
			}
		}
	}
</script>

<style lang="scss" scoped>
 
</style>
